{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "is_executing": true,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022/09/07 04:43:30 PM, INFO, mealpy.bio_based.BBO.BaseBBO: Solving single objective optimization problem.\n",
      "2022/09/07 04:43:30 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Custom Squared Func, Epoch: 1, Current best: 14581.515353877252, Global best: 14581.515353877252, Runtime: 0.00799 seconds\n",
      "2022/09/07 04:43:30 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Custom Squared Func, Epoch: 2, Current best: 9204.420788032765, Global best: 9204.420788032765, Runtime: 0.00729 seconds\n",
      "2022/09/07 04:43:30 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Custom Squared Func, Epoch: 3, Current best: 9204.420788032765, Global best: 9204.420788032765, Runtime: 0.00799 seconds\n",
      "2022/09/07 04:43:30 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Custom Squared Func, Epoch: 4, Current best: 7786.761565120972, Global best: 7786.761565120972, Runtime: 0.00701 seconds\n",
      "2022/09/07 04:43:30 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Custom Squared Func, Epoch: 5, Current best: 7784.671471995334, Global best: 7784.671471995334, Runtime: 0.00694 seconds\n",
      "2022/09/07 04:43:30 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Custom Squared Func, Epoch: 6, Current best: 5511.967605728147, Global best: 5511.967605728147, Runtime: 0.00740 seconds\n",
      "2022/09/07 04:43:30 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Custom Squared Func, Epoch: 7, Current best: 4546.696916395481, Global best: 4546.696916395481, Runtime: 0.00642 seconds\n",
      "2022/09/07 04:43:30 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Custom Squared Func, Epoch: 8, Current best: 3398.664841411833, Global best: 3398.664841411833, Runtime: 0.00695 seconds\n",
      "2022/09/07 04:43:31 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Custom Squared Func, Epoch: 9, Current best: 2943.7708598934983, Global best: 2943.7708598934983, Runtime: 0.00644 seconds\n",
      "2022/09/07 04:43:31 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Custom Squared Func, Epoch: 10, Current best: 2028.8789626240382, Global best: 2028.8789626240382, Runtime: 0.00629 seconds\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pop_size': 50, 'epoch': 10, 'p_m': 0.01, 'elites': 2}\n",
      "BaseBBO\n",
      "Custom Squared Func\n",
      "[array([  9.07907415,   0.50023341, -19.6598944 ,   3.64678794,\n",
      "       -16.9819883 , -15.18229023, -19.44038321, -24.07676543,\n",
      "         6.01888518,   5.80109097]), [2028.8789626240382, [2028.8789626240382]]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from opfunu.cec_based.cec2017 import F292017\n",
    "from mealpy.bio_based import BBO\n",
    "from mealpy.utils.problem import Problem\n",
    "\n",
    "\n",
    "#### Solve problem with dictionary definition (custom fitness function)\n",
    "\n",
    "def fitness(solution):\n",
    "    return np.sum(solution**2)\n",
    "\n",
    "p0 = {\n",
    "    \"lb\": [-100, ]*10,\n",
    "    \"ub\": [100,]*10,\n",
    "    \"minmax\": \"min\",\n",
    "    \"fit_func\": fitness,\n",
    "    \"name\": \"Custom Squared Func\"\n",
    "}\n",
    "\n",
    "model = BBO.BaseBBO(epoch=10, pop_size=50)\n",
    "best_position, best_fitness = model.solve(p0)\n",
    "print(model.get_parameters())\n",
    "print(model.get_name())\n",
    "print(model.problem.get_name())\n",
    "print(model.get_attributes()[\"solution\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022/09/07 04:43:40 PM, INFO, mealpy.bio_based.BBO.BaseBBO: Solving single objective optimization problem.\n",
      "2022/09/07 04:43:40 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: F18 Opfunu, Epoch: 1, Current best: 204773019883805.62, Global best: 204773019883805.62, Runtime: 0.03413 seconds\n",
      "2022/09/07 04:43:40 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: F18 Opfunu, Epoch: 2, Current best: 102417985288385.11, Global best: 102417985288385.11, Runtime: 0.03688 seconds\n",
      "2022/09/07 04:43:40 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: F18 Opfunu, Epoch: 3, Current best: 2740723033686.659, Global best: 2740723033686.659, Runtime: 0.03751 seconds\n",
      "2022/09/07 04:43:40 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: F18 Opfunu, Epoch: 4, Current best: 2740723033686.659, Global best: 2740723033686.659, Runtime: 0.03826 seconds\n",
      "2022/09/07 04:43:40 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: F18 Opfunu, Epoch: 5, Current best: 2740723033686.659, Global best: 2740723033686.659, Runtime: 0.03605 seconds\n",
      "2022/09/07 04:43:40 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: F18 Opfunu, Epoch: 6, Current best: 66290647239.707695, Global best: 66290647239.707695, Runtime: 0.03706 seconds\n",
      "2022/09/07 04:43:40 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: F18 Opfunu, Epoch: 7, Current best: 66290647239.707695, Global best: 66290647239.707695, Runtime: 0.03756 seconds\n",
      "2022/09/07 04:43:41 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: F18 Opfunu, Epoch: 8, Current best: 63389704217.251144, Global best: 63389704217.251144, Runtime: 0.03741 seconds\n",
      "2022/09/07 04:43:41 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: F18 Opfunu, Epoch: 9, Current best: 55011385229.978836, Global best: 55011385229.978836, Runtime: 0.03784 seconds\n",
      "2022/09/07 04:43:41 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: F18 Opfunu, Epoch: 10, Current best: 50682990668.75354, Global best: 50682990668.75354, Runtime: 0.03950 seconds\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pop_size': 50, 'epoch': 10, 'p_m': 0.01, 'elites': 2}\n",
      "BaseBBO\n",
      "F18 Opfunu\n",
      "[array([-23.79341222, -87.50827064,   7.15814106, -99.73557832,\n",
      "       -23.17147867,  25.84902512,  40.92513085, -32.31196728,\n",
      "         6.67457407, -21.0395546 , -83.39866211,  15.64131801,\n",
      "       -12.74583933,  89.17124067, -59.34527515, -33.56517792,\n",
      "       -56.2632836 ,  22.7585842 ,  49.33002726,  63.14000818,\n",
      "        33.77699522, -88.86425812, -84.36679789,  44.01984638,\n",
      "        10.44851552, -44.26273415,  23.78171404,  80.61958209,\n",
      "        19.23729253, -24.57810684]), [50682990668.75354, [50682990668.75354]]]\n"
     ]
    }
   ],
   "source": [
    "#### Solve problem with dictionary definition (function called from other library)\n",
    "\n",
    "f18 = F292017(30, f_bias=0)\n",
    "\n",
    "def fitness(solution):\n",
    "    return f18.evaluate(solution)\n",
    "\n",
    "p1 = {\n",
    "    \"lb\": f18.lb.tolist(),\n",
    "    \"ub\": f18.ub.tolist(),\n",
    "    \"minmax\": \"min\",\n",
    "    \"fit_func\": fitness,\n",
    "    \"name\": \"F18 Opfunu\"\n",
    "}\n",
    "\n",
    "model = BBO.BaseBBO(epoch=10, pop_size=50)\n",
    "best_position, best_fitness = model.solve(p1)\n",
    "print(model.get_parameters())\n",
    "print(model.get_name())\n",
    "print(model.problem.get_name())\n",
    "print(model.get_attributes()[\"solution\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022/09/07 04:43:46 PM, INFO, mealpy.bio_based.BBO.BaseBBO: Solving 2-objective optimization problem with weights: [0.5 0.5].\n",
      "2022/09/07 04:43:46 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 1, Current best: -639.0101339057069, Global best: -639.0101339057069, Runtime: 0.00724 seconds\n",
      "2022/09/07 04:43:46 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 2, Current best: -1106.5857308933341, Global best: -1106.5857308933341, Runtime: 0.00725 seconds\n",
      "2022/09/07 04:43:46 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 3, Current best: -1201.2737660613725, Global best: -1201.2737660613725, Runtime: 0.00710 seconds\n",
      "2022/09/07 04:43:46 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 4, Current best: -1376.4726679917162, Global best: -1376.4726679917162, Runtime: 0.00785 seconds\n",
      "2022/09/07 04:43:46 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 5, Current best: -1376.4726679917162, Global best: -1376.4726679917162, Runtime: 0.00826 seconds\n",
      "2022/09/07 04:43:46 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 6, Current best: -1788.481681293696, Global best: -1788.481681293696, Runtime: 0.00763 seconds\n",
      "2022/09/07 04:43:46 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 7, Current best: -1811.6698153439925, Global best: -1811.6698153439925, Runtime: 0.00761 seconds\n",
      "2022/09/07 04:43:46 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 8, Current best: -1834.2370994649418, Global best: -1834.2370994649418, Runtime: 0.00812 seconds\n",
      "2022/09/07 04:43:46 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 9, Current best: -1861.2812051557746, Global best: -1861.2812051557746, Runtime: 0.00848 seconds\n",
      "2022/09/07 04:43:46 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 10, Current best: -1916.6227142898076, Global best: -1916.6227142898076, Runtime: 0.00702 seconds\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pop_size': 50, 'epoch': 10, 'p_m': 0.01, 'elites': 2}\n",
      "BaseBBO\n",
      "Multi-objective Child Squared\n",
      "[array([-9.67724544, -9.99157832, -9.3046842 , -9.52441215, -9.82757643,\n",
      "       -6.72744558,  9.3410694 ,  1.67834795, -2.56086577, -2.10842729,\n",
      "       -5.13991338,  2.13760994, -2.29908598,  0.78583363,  0.48837507,\n",
      "        2.90762795, -0.69510098, -1.78815647, -1.1821323 ,  4.9879149 ]), [-1916.6227142898076, [689.2296305513515, -4522.475059130967]]]\n"
     ]
    }
   ],
   "source": [
    "#### Solve problem with custom child class of Problem class.\n",
    "\n",
    "class Squared(Problem):\n",
    "    def __init__(self, lb, ub, minmax, name=\"Multi-objective Child Squared\", **kwargs):\n",
    "        super().__init__(lb, ub, minmax, **kwargs)\n",
    "        self.name = name\n",
    "\n",
    "    def fit_func(self, solution):\n",
    "        return [np.sum(solution ** 2), np.sum(solution[:5]**3)]\n",
    "\n",
    "\n",
    "p2 = Squared(lb=[-10, ] * 20, ub=[10, ] * 20, minmax=\"min\", obj_weights=[0.5, 0.5])\n",
    "\n",
    "model = BBO.BaseBBO(epoch=10, pop_size=50)\n",
    "best_position, best_fitness = model.solve(p2)\n",
    "\n",
    "print(model.get_parameters())\n",
    "print(model.get_name())\n",
    "print(model.problem.get_name())\n",
    "print(model.get_attributes()[\"solution\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: Solving 2-objective optimization problem with weights: [0.2 0.8].\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 1, Current best: -1230.4066755190734, Global best: -1230.4066755190734, Runtime: 0.00694 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 2, Current best: -1401.1270522838477, Global best: -1401.1270522838477, Runtime: 0.00791 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 3, Current best: -1744.2019242186652, Global best: -1744.2019242186652, Runtime: 0.00828 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 4, Current best: -1844.0372194561005, Global best: -1844.0372194561005, Runtime: 0.00712 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 5, Current best: -2367.243586409994, Global best: -2367.243586409994, Runtime: 0.00706 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 6, Current best: -2447.925973638658, Global best: -2447.925973638658, Runtime: 0.00729 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 7, Current best: -2447.925973638658, Global best: -2447.925973638658, Runtime: 0.00678 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 8, Current best: -2739.3722462989053, Global best: -2739.3722462989053, Runtime: 0.00712 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 9, Current best: -2739.3722462989053, Global best: -2739.3722462989053, Runtime: 0.00826 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 10, Current best: -2749.670085530241, Global best: -2749.670085530241, Runtime: 0.00732 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: Solving 2-objective optimization problem with weights: [0.7 0.3].\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 1, Current best: -2749.670085530241, Global best: -2749.670085530241, Runtime: 0.00902 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 2, Current best: -2749.670085530241, Global best: -2749.670085530241, Runtime: 0.00839 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 3, Current best: -2749.670085530241, Global best: -2749.670085530241, Runtime: 0.00679 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 4, Current best: -2749.670085530241, Global best: -2749.670085530241, Runtime: 0.00747 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 5, Current best: -2749.670085530241, Global best: -2749.670085530241, Runtime: 0.00751 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 6, Current best: -2749.670085530241, Global best: -2749.670085530241, Runtime: 0.00811 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 7, Current best: -2749.670085530241, Global best: -2749.670085530241, Runtime: 0.00694 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 8, Current best: -2749.670085530241, Global best: -2749.670085530241, Runtime: 0.00837 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 9, Current best: -2749.670085530241, Global best: -2749.670085530241, Runtime: 0.00926 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 10, Current best: -2749.670085530241, Global best: -2749.670085530241, Runtime: 0.00804 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: Solving 2-objective optimization problem with weights: [1.  2.5].\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 1, Current best: -8443.603547202229, Global best: -8443.603547202229, Runtime: 0.00903 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 2, Current best: -8444.768258309576, Global best: -8444.768258309576, Runtime: 0.00800 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 3, Current best: -8451.42974211353, Global best: -8451.42974211353, Runtime: 0.00871 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 4, Current best: -8493.562659187413, Global best: -8493.562659187413, Runtime: 0.00730 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 5, Current best: -8587.435799342604, Global best: -8587.435799342604, Runtime: 0.00771 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 6, Current best: -8587.435799342604, Global best: -8587.435799342604, Runtime: 0.00808 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 7, Current best: -8620.66685162721, Global best: -8620.66685162721, Runtime: 0.00818 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 8, Current best: -8634.392872025534, Global best: -8634.392872025534, Runtime: 0.00758 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 9, Current best: -8648.028949043546, Global best: -8648.028949043546, Runtime: 0.00780 seconds\n",
      "2022/09/07 04:43:51 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 10, Current best: -8664.801810410721, Global best: -8664.801810410721, Runtime: 0.00963 seconds\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pop_size': 50, 'epoch': 10, 'p_m': 0.01, 'elites': 2}\n",
      "BaseBBO\n",
      "Multi-objective Child Squared\n",
      "[array([-8.02732808e+00, -9.37622499e+00, -9.08461678e+00, -8.75849098e+00,\n",
      "       -9.39578623e+00,  1.95081832e+00, -1.60468271e+00, -8.82865065e-03,\n",
      "        2.87661700e+00, -2.00671154e+00,  7.20979839e+00,  2.14412925e+00,\n",
      "       -2.66801766e+00, -2.61140225e+00, -4.14553831e+00, -3.40554844e+00,\n",
      "       -1.53504728e+00,  9.72161113e+00,  1.05314071e+00,  2.54130985e+00]), [-2749.670085530241, [622.2893348345749, -3592.6599406214445]]]\n",
      "{'pop_size': 50, 'epoch': 10, 'p_m': 0.01, 'elites': 2}\n",
      "BaseBBO\n",
      "Multi-objective Child Squared\n",
      "[array([-8.02732808e+00, -9.37622499e+00, -9.08461678e+00, -8.75849098e+00,\n",
      "       -9.39578623e+00,  1.95081832e+00, -1.60468271e+00, -8.82865065e-03,\n",
      "        2.87661700e+00, -2.00671154e+00,  7.20979839e+00,  2.14412925e+00,\n",
      "       -2.66801766e+00, -2.61140225e+00, -4.14553831e+00, -3.40554844e+00,\n",
      "       -1.53504728e+00,  9.72161113e+00,  1.05314071e+00,  2.54130985e+00]), [-2749.670085530241, [622.2893348345749, -3592.6599406214445]]]\n",
      "{'pop_size': 50, 'epoch': 10, 'p_m': 0.01, 'elites': 2}\n",
      "BaseBBO\n",
      "Multi-objective Child Squared\n",
      "[array([-8.31591931e+00, -9.37622499e+00, -9.08461678e+00, -8.75849098e+00,\n",
      "       -9.39578623e+00,  1.95081832e+00, -1.60468271e+00, -8.82865065e-03,\n",
      "        2.87661700e+00, -1.55497233e-01,  1.60343235e+00,  2.14412925e+00,\n",
      "       -2.66801766e+00, -2.61140225e+00,  6.42360096e-01,  1.46438831e+00,\n",
      "       -1.53504728e+00,  2.92182602e+00,  1.05314071e+00,  2.54130985e+00]), [-8664.801810410721, [461.3940993961353, -3650.4783639227426]]]\n"
     ]
    }
   ],
   "source": [
    "#### Solve multiple problems using the same model\n",
    "\n",
    "p3 = Squared(lb=[-10, ] * 20, ub=[10, ] * 20, minmax=\"min\", obj_weights=[0.2, 0.8])\n",
    "p4 = Squared(lb=[-10, ] * 20, ub=[10, ] * 20, minmax=\"min\", obj_weights=[0.7, 0.3])\n",
    "p5 = Squared(lb=[-10, ] * 20, ub=[10, ] * 20, minmax=\"min\", obj_weights=[1.0, 2.5])\n",
    "\n",
    "model = BBO.BaseBBO(epoch=10, pop_size=50)\n",
    "\n",
    "for prob in [p3, p4, p5]:\n",
    "    best_position, best_fitness = model.solve(prob)\n",
    "    print(model.get_parameters())\n",
    "    print(model.get_name())\n",
    "    print(model.problem.get_name())\n",
    "    print(model.get_attributes()[\"solution\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: Solving 2-objective optimization problem with weights: [0.2 0.8].\n",
      "2022/09/07 04:44:11 PM, WARNING, mealpy.bio_based.BBO.BaseBBO: Stopping condition mode: Function Evaluation, with maximum value is: 10000\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 1, Current best: -1598.018802584533, Global best: -1598.018802584533, Runtime: 0.00785 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 2, Current best: -2462.3633459197813, Global best: -2462.3633459197813, Runtime: 0.00734 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 3, Current best: -2462.3633459197813, Global best: -2462.3633459197813, Runtime: 0.00775 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 4, Current best: -2533.419767277882, Global best: -2533.419767277882, Runtime: 0.00764 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 5, Current best: -2533.419767277882, Global best: -2533.419767277882, Runtime: 0.00755 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 6, Current best: -2897.9311898569918, Global best: -2897.9311898569918, Runtime: 0.00811 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 7, Current best: -3127.2425923017545, Global best: -3127.2425923017545, Runtime: 0.00762 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 8, Current best: -3143.0483585257425, Global best: -3143.0483585257425, Runtime: 0.00724 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 9, Current best: -3153.539311995865, Global best: -3153.539311995865, Runtime: 0.00676 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 10, Current best: -3167.2461190279873, Global best: -3167.2461190279873, Runtime: 0.00664 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: Solving 2-objective optimization problem with weights: [0.2 0.8].\n",
      "2022/09/07 04:44:11 PM, WARNING, mealpy.bio_based.BBO.BaseBBO: Stopping condition mode: Early Stopping, with maximum value is: 20\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 1, Current best: -3178.964381387346, Global best: -3178.964381387346, Runtime: 0.00905 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 2, Current best: -3181.4750199953264, Global best: -3181.4750199953264, Runtime: 0.00770 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 3, Current best: -3191.5233480637307, Global best: -3191.5233480637307, Runtime: 0.00684 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 4, Current best: -3194.80223080629, Global best: -3194.80223080629, Runtime: 0.00777 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 5, Current best: -3195.1560170030416, Global best: -3195.1560170030416, Runtime: 0.00854 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 6, Current best: -3195.5453271173856, Global best: -3195.5453271173856, Runtime: 0.00775 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 7, Current best: -3195.899113314137, Global best: -3195.899113314137, Runtime: 0.00803 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 8, Current best: -3200.9946028839354, Global best: -3200.9946028839354, Runtime: 0.00757 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 9, Current best: -3200.9946028839354, Global best: -3200.9946028839354, Runtime: 0.00776 seconds\n",
      "2022/09/07 04:44:11 PM, INFO, mealpy.bio_based.BBO.BaseBBO: >Problem: Multi-objective Child Squared, Epoch: 10, Current best: -3200.9946028839354, Global best: -3200.9946028839354, Runtime: 0.00776 seconds\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pop_size': 50, 'epoch': 10, 'p_m': 0.01, 'elites': 2}\n",
      "BaseBBO\n",
      "Multi-objective Child Squared\n",
      "[array([-9.55419741, -9.61431155, -9.70756232, -8.11652823, -9.73082702,\n",
      "        3.89935094,  1.12214235,  8.09016845, -5.09000091,  1.39496821,\n",
      "       -8.80563183,  2.01694526, -1.22646915,  2.66475621,  5.38294927,\n",
      "        0.2019051 ,  1.62902558,  1.73065186, -4.05121013, -1.07907208]), [-3167.2461190279873, [690.745339439276, -4131.743983644803]]]\n",
      "{'pop_size': 50, 'epoch': 10, 'p_m': 0.01, 'elites': 2}\n",
      "BaseBBO\n",
      "Multi-objective Child Squared\n",
      "[array([-9.55319437, -9.61431155, -9.70756232, -8.11652823, -9.73082702,\n",
      "       -2.27065296,  1.12214235, -4.3578315 ,  5.46310279,  1.39496821,\n",
      "        1.647767  , -0.59379004, -1.22646915,  2.6397802 , -0.19254254,\n",
      "       -1.71900299,  1.62902558,  1.73065186,  1.96446189, -1.07907208]), [-3200.9946028839354, [520.9043125266154, -4131.469331736573]]]\n"
     ]
    }
   ],
   "source": [
    "#### Solve problem with difference termination\n",
    "\n",
    "p6 = Squared(lb=[-10, ] * 20, ub=[10, ] * 20, minmax=\"min\", obj_weights=[0.2, 0.8])\n",
    "\n",
    "term1 = {\n",
    "    \"mode\": \"FE\",\n",
    "    \"quantity\": 10000,\n",
    "}\n",
    "term2 = {\n",
    "    \"mode\": \"ES\",\n",
    "    \"quantity\": 20,\n",
    "}\n",
    "\n",
    "model = BBO.BaseBBO(epoch=10, pop_size=50)\n",
    "\n",
    "\n",
    "for term in [term1, term2]:\n",
    "    best_position, best_fitness = model.solve(p6, termination=term)\n",
    "    print(model.get_parameters())\n",
    "    print(model.get_name())\n",
    "    print(model.problem.get_name())\n",
    "    print(model.get_attributes()[\"solution\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}